import { defineStore } from 'pinia'
import { ref } from 'vue'
import {  consultPlanApi } from '@/api'

export const useConsultPlanStore = defineStore('consultPlan', () => {
  const options = ref<any[]>([])

  const fetchOptions = async () => {
    if(options.value.length>0){
      return options.value
    }else{
      options.value =await getProcessedOptions()
      return options.value
    }
  }
  const getProcessedOptions = async() => {
    try {
      const { data } = await consultPlanApi.getConsultPlanOptions()

      const processedData = data.map((item: any) => ({
        label:item.title,
        key: item.id,
      }))
      return processedData 
    } catch (error) {
      console.error('獲取診金方案選項表失敗:', error)
    }
  }

  return {
    options,
    fetchOptions,
  }
})
